package com.dongdongshop.controller;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.data.Result;
import com.dongdongshop.data.ResultEnum;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.GoodsService;
import com.dongdongshop.service.ItemService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("goodsController")
public class GoodsController {

    @DubboReference
    private GoodsService goodsService;

    @DubboReference
    private ItemService itemService;

    @RequestMapping("saveGoods")
    @ResponseBody
    public Result saveGoods(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc, String propertys, String thp1, String thp2) {
        try {
            List<TbItem> items = JSONObject.parseArray(propertys,TbItem.class);
            Subject subject = SecurityUtils.getSubject();
            TbSeller tbSeller =(TbSeller) subject.getPrincipal();
            items.stream().map(s -> {s.setSellerId(tbGoods.getSellerId());s.setSellerId(tbSeller.getSellerId());s.setSeller(tbSeller.getName()); int i = itemService.insert(s); return i;}).collect(Collectors.toList());
            tbGoods.setAuditStatus("0");
            tbGoods.setIsMarketable("1");
            tbGoods.setSellerId(tbSeller.getSellerId());
            goodsService.saveGoods(tbGoods,tbGoodsDesc,tbSeller.getName(),thp1,thp2);
            return Result.respones(ResultEnum.SUCCESS);
        }catch (Exception e) {
            return Result.respones(ResultEnum.ERROR);
        }
    }
}
